using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Security.Permissions;
namespace Circus.CodeEditor
{
	internal class Win32
	{
	
		public struct GdiRect
		{
			public int left;
			public int top;
			public int right;
			public int bottom;
			public GdiRect(int l, int t, int r, int b)
			{
				this.left = l;
				this.top = t;
				this.right = r;
				this.bottom = b;
			}
			public GdiRect(Rectangle rect)
			{
				this.left = rect.Left;
				this.top = rect.Top;
				this.right = rect.Right;
				this.bottom = rect.Bottom;
			}
			public Rectangle ToRectangle()
			{
				return new Rectangle(this.left, this.top, this.right - this.left, this.bottom - this.top);
			}
		}
		[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
		public class LOGFONT
		{
			public int lfHeight = 0;
			public int lfWidth = 0;
			public int lfEscapement = 0;
			public int lfOrientation = 0;
			public int lfWeight = 0;
			public byte lfItalic = 0;
			public byte lfUnderline = 0;
			public byte lfStrikeOut = 0;
			public byte lfCharSet = 0;
			public byte lfOutPrecision = 0;
			public byte lfClipPrecision = 0;
			public byte lfQuality = 0;
			public byte lfPitchAndFamily = 0;
			[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
			public string lfFaceName = null;
		}
		public struct GdiSize
		{
			public int cx;
			public int cy;
			public GdiSize(int cx, int cy)
			{
				this.cx = cx;
				this.cy = cy;
			}
		}
		public struct GdiPoint
		{
			public int X;
			public int Y;
			public GdiPoint(int x, int y)
			{
				this.X = x;
				this.Y = y;
			}
		}
		public struct ColorRef
		{
			public byte rgbRed;
			public byte rgbGreen;
			public byte rgbBlue;
			public byte rgbReserved;
			public Color Color
			{
				get
				{
					return Color.FromArgb((int)this.rgbRed, (int)this.rgbGreen, (int)this.rgbBlue);
				}
			}
		}
		public struct MOUSEHOOKSTRUCT
		{
			public Win32.GdiPoint Pt;
			public IntPtr hwnd;
			public uint wHitTestCode;
			public IntPtr dwExtraInfo;
		}
		[StructLayout(LayoutKind.Explicit)]
		public struct TextMetrics
		{
			[FieldOffset(0)]
			public int Height;
			[FieldOffset(4)]
			public int Accent;
			[FieldOffset(8)]
			public int Descent;
			[FieldOffset(12)]
			public int publicLeading;
			[FieldOffset(16)]
			public int ExternalLeading;
			[FieldOffset(20)]
			public int AveCharWidth;
			[FieldOffset(24)]
			public int MaxCharWidth;
			[FieldOffset(28)]
			public int Weight;
			[FieldOffset(32)]
			public int Overhang;
			[FieldOffset(36)]
			public int DigitizedAspectX;
			[FieldOffset(40)]
			public int DigitizedAspectY;
			[FieldOffset(44)]
			public byte FirstChar;
			[FieldOffset(45)]
			public byte LastChar;
			[FieldOffset(46)]
			public byte DefaultChar;
			[FieldOffset(57)]
			public byte BreakChar;
			[FieldOffset(58)]
			public byte Italic;
			[FieldOffset(49)]
			public byte Underlined;
			[FieldOffset(50)]
			public byte StruckOut;
			[FieldOffset(51)]
			public byte PitchAndFamily;
			[FieldOffset(52)]
			public byte CharSet;
			[FieldOffset(52)]
			private int Reserved;
		}
		public struct MSG
		{
			public IntPtr hwnd;
			public int message;
			public IntPtr wparam;
			public IntPtr lparam;
			public uint time;
			public Win32.GdiPoint Pt;
		}
		[StructLayout(LayoutKind.Explicit)]
		public struct ScrollInfo
		{
			[FieldOffset(0)]
			public uint cbSize;
			[FieldOffset(4)]
			public uint fMask;
			[FieldOffset(8)]
			public int nMin;
			[FieldOffset(12)]
			public int nMax;
			[FieldOffset(16)]
			public uint nPage;
			[FieldOffset(20)]
			public int nPos;
			[FieldOffset(24)]
			public int nTrackPos;
		}
		[StructLayout(LayoutKind.Explicit)]
		public struct XFORM
		{
			[FieldOffset(0)]
			public float eM11;
			[FieldOffset(4)]
			public float eM12;
			[FieldOffset(8)]
			public float eM21;
			[FieldOffset(12)]
			public float eM22;
			[FieldOffset(16)]
			public float eDx;
			[FieldOffset(20)]
			public float eDy;
			public XFORM(float m11, float m12, float m21, float m22, float dx, float dy)
			{
				this.eM11 = m11;
				this.eM12 = m12;
				this.eM21 = m21;
				this.eM22 = m22;
				this.eDx = dx;
				this.eDy = dy;
			}
		}
		[StructLayout(LayoutKind.Explicit)]
		public struct INITCOMMONCONTROLSEX
		{
			[FieldOffset(0)]
			public int dwSize;
			[FieldOffset(8)]
			public int dwICC;
		}
		public struct CANDIDATEFORM
		{
			public int dwIndex;
			public int dwStyle;
			public Win32.GdiPoint ptCurrentPos;
			public Win32.GdiRect rcArea;
		}
		public struct COMPOSITIONFORM
		{
			public uint dwStyle;
			public Win32.GdiPoint ptCurrentPos;
			public Win32.GdiRect rcArea;
		}
		public const int TMPF_FIXED_PITCH = 1;
		public const int TMPF_VECTOR = 2;
		public const int TMPF_DEVICE = 8;
		public const int TMPF_TRUETYPE = 4;
		public const int OPAQUE = 2;
		public const int TRANSPARENT = 1;
		public const int GM_COMPATIBLE = 1;
		public const int GM_ADVANCED = 2;
		public const int HOLLOW_BRUSH = 5;
		public const int DT_TOP = 0;
		public const int DT_LEFT = 0;
		public const int DT_CENTER = 1;
		public const int DT_RIGHT = 2;
		public const int DT_VCENTER = 4;
		public const int DT_BOTTOM = 8;
		public const int DT_SINGLELINE = 32;
		public const int DT_NOCLIP = 256;
		public const int DT_CALCRECT = 1024;
		public const int DT_NOPREFIX = 2048;
		public const int DT_HIDEPREFIX = 1048576;
		public const int DT_PATH_ELLIPSIS = 16384;
		public const int DT_END_ELLIPSIS = 32768;
		public const int DT_RTLREADING = 131072;
		public const int DT_WORD_ELLIPSIS = 262144;
		public const int ETO_OPAQUE = 2;
		public const int ETO_CLIPPED = 4;
		public const int PS_SOLID = 0;
		public const int PS_DASH = 1;
		public const int PS_DOT = 2;
		public const int PS_DASHDOT = 3;
		public const int PS_DASHDOTDOT = 4;
		public const int BS_SOLID = 0;
		public const int BS_PATTERN = 3;
		public const int BDR_RAISEDOUTER = 1;
		public const int BDR_SUNKENOUTER = 2;
		public const int BDR_RAISEDINNER = 4;
		public const int BDR_SUNKENINNER = 8;
		public const int BDR_OUTER = 3;
		public const int BDR_INNER = 12;
		public const int BDR_RAISED = 5;
		public const int BDR_SUNKEN = 10;
		public const int EDGE_RAISED = 5;
		public const int EDGE_SUNKEN = 10;
		public const int EDGE_ETCHED = 6;
		public const int EDGE_BUMP = 9;
		public const int BF_LEFT = 1;
		public const int BF_TOP = 2;
		public const int BF_RIGHT = 4;
		public const int BF_BOTTOM = 8;
		public const int BF_MIDDLE = 2048;
		public const int BF_SOFT = 4096;
		public const int BF_ADJUST = 8192;
		public const int BF_FLAT = 16384;
		public const int BF_MONO = 32768;
		public const int RGN_AND = 1;
		public const int RGN_OR = 2;
		public const int RGN_XOR = 3;
		public const int RGN_DIFF = 4;
		public const int RGN_COPY = 5;
		public const int RGN_MIN = 1;
		public const int RGN_MAX = 5;
		public const int SB_HORZ = 0;
		public const int SB_VERT = 1;
		public const int SIF_RANGE = 1;
		public const int SIF_PAGE = 2;
		public const int SIF_POS = 4;
		public const int SIF_DISABLENOSCROLL = 8;
		public const int SIF_TRACKPOS = 16;
		public const int SB_LINEUP = 0;
		public const int SB_LINELEFT = 0;
		public const int SB_LINEDOWN = 1;
		public const int SB_LINERIGHT = 1;
		public const int SB_PAGEUP = 2;
		public const int SB_PAGELEFT = 2;
		public const int SB_PAGEDOWN = 3;
		public const int SB_PAGERIGHT = 3;
		public const int SB_THUMBPOSITION = 4;
		public const int SB_THUMBTRACK = 5;
		public const int SB_TOP = 6;
		public const int SB_LEFT = 6;
		public const int SB_BOTTOM = 7;
		public const int SB_RIGHT = 7;
		public const int SB_ENDSCROLL = 8;
		public const int Scroll_Size = 28;
		public const int SM_CYVSCROLL = 20;
		public const int SM_CXHSCROLL = 21;
		public const int WSB_PROP_HSTYLE = 512;
		public const int WSB_PROP_VSTYLE = 256;
		public const int FSB_FLAT_MODE = 2;
		public const int WS_VSCROLL = 2097152;
		public const int WS_HSCROLL = 1048576;
		public const int WM_CLOSEDROPPED = 1025;
		public const int WM_MOUSEMOVE = 512;
		public const int WM_MINCLICK = 513;
		public const int WM_MAXCLICK = 520;
		public const int WM_MINNCCLICK = 161;
		public const int WM_MAXNCCLICK = 169;
		public const int WM_LBUTTONDOWN = 513;
		public const int WM_NCLBUTTONDOWN = 161;
		public const int WM_NCPAINT = 133;
		public const int WM_HSCROLL = 276;
		public const int WM_VSCROLL = 277;
		public const int WM_KEYDOWN = 256;
		public const int WM_CHAR = 258;
		public const int WM_SYSKEYDOWN = 260;
		public const int WM_SETCURSOR = 32;
		public const int WM_LBUTTONUP = 514;
		public const int WM_NCACTIVATE = 134;
		public const int WM_IME_STARTCOMPOSITION = 269;
		public const int IMN_SETOPENSTATUS = 8;
		public const int IMN_SETCOMPOSITIONWINDOW = 11;
		public const int IMC_GETCOMPOSITIONWINDOW = 11;
		public const int WM_IME_NOTIFY = 642;
		public const int WM_IME_CONTROL = 643;
		public const int WM_IME_COMPOSITION = 271;
		public const int GCS_RESULTSTR = 2048;
		public const int WM_GETTEXT = 13;
		public const int WM_SETTEXT = 12;
		public const int WM_INITDIALOG = 272;
		public const int WM_SIZE = 5;
		public const int WM_NCDESTROY = 130;
		public const int WM_DESTROY = 2;
		public const int WM_CLOSE = 16;
		public const int WM_CUT = 768;
		public const int WM_COPY = 769;
		public const int WM_PASTE = 770;
		public const int WM_CLEAR = 771;
		public const int WM_CANUNDO = 198;
		public const int WM_UNDO = 772;
		public const int WM_CONTEXTMENU = 123;
		public const int LOGPIXELSX = 88;
		public const int LOGPIXELSY = 90;
		public const int SM_DBCSENABLED = 42;
		public const int SW_SHOW = 5;
		public const int SW_HIDE = 0;
		public const int SWP_NOSIZE = 1;
		public const int SWP_NOMOVE = 2;
		public const int SWP_NOZORDER = 4;
		public const int SWP_NOACTIVATE = 16;
		public const int SWP_SHOWWINDOW = 64;
		public const int HWND_TOPMOST = -1;
		public const int GWL_WNDPROC = -4;
		public const int WS_EX_CLIENTEDGE = 512;
		public const int WS_BORDER = 8388608;
		public const int WS_EX_DLGMODALFRAME = 1;
		public const int WS_EX_WINDOWEDGE = 256;
		public const int WH_MOUSE = 7;
		public const int WH_CALLWNDPROC = 4;
		public const int WH_GETMESSAGE = 3;
		public const int FW_NORMAL = 400;
		public const int FW_BOLD = 700;
		public const int DEFAULT_CHARSET = 1;
		public const int DEFAULT_QUALITY = 0;
		public const int OUT_DEFAULT_PRECIS = 0;
		public const int CLIP_DEFAULT_PRECIS = 0;
		public const int DEFAULT_PITCH = 0;
		public const int MB_ICONHAND = 16;
		public const int CFS_POINT = 2;
		private Win32()
		{
		}
		[DllImport("comctl32.dll")]
		[return: MarshalAs(UnmanagedType.Bool)]
		private static extern bool InitCommonControlsEx(ref Win32.INITCOMMONCONTROLSEX iic);
		public static bool InitCommonControls(int icc)
		{
			Win32.INITCOMMONCONTROLSEX iNITCOMMONCONTROLSEX = default(Win32.INITCOMMONCONTROLSEX);
			iNITCOMMONCONTROLSEX.dwSize = 16;
			iNITCOMMONCONTROLSEX.dwICC = icc;
			return Win32.InitCommonControlsEx(ref iNITCOMMONCONTROLSEX);
		}
		[EnvironmentPermission(SecurityAction.LinkDemand, Unrestricted = true)]
		[DllImport("gdi32.dll", CharSet = CharSet.Auto)]
		[return: MarshalAs(UnmanagedType.Bool)]
		private static extern bool GetCharABCWidths(IntPtr hdc, int iFirstChar, int iLastChar, IntPtr buffer);

		[DllImport("user32.dll")]
		[return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool DrawEdge(IntPtr hdc, ref Win32.GdiRect rect, System.Windows.Forms.Border3DStyle edge, System.Windows.Forms.Border3DSide flags);

		[EnvironmentPermission(SecurityAction.LinkDemand, Unrestricted = true)]
		[DllImport("gdi32.dll", CharSet = CharSet.Auto)]
		private static extern int ExtTextOut(IntPtr hdc, int x, int y, int options, ref Win32.GdiRect rect, string s, int len, IntPtr buffer);
		[DllImport("user32.dll", CharSet = CharSet.Auto)]
		private static extern int DrawText(IntPtr hdc, string s, int len, ref Win32.GdiRect rect, int format);
		[DllImport("gdi32.dll", CharSet = CharSet.Auto)]
		private static extern IntPtr CreateFont(int nHeight, int nWidth, int nEscapement, int nOrientaion, int fnWeight, uint fdwItalic, uint fdwUnderline, uint fdwStrikeOut, uint fdwCharSet, uint fdwOutputPrecision, uint fdwClipPrecision, uint fdwQuality, uint fdwPitchAndFamily, string lpszFace);
		[DllImport("user32.dll")]
		private static extern int SetScrollInfo(IntPtr hwnd, int barFlag, ref Win32.ScrollInfo scrollInfo, [MarshalAs(UnmanagedType.Bool)] bool redraw);
		[DllImport("user32.dll")]
		private static extern int GetScrollInfo(IntPtr hwnd, int barFlag, ref Win32.ScrollInfo scrollInfo);
		[DllImport("user32.dll")]
		[return: MarshalAs(UnmanagedType.Bool)]
		private static extern bool ScrollWindow(IntPtr handle, int x, int y, ref Win32.GdiRect r, IntPtr clipR);
		[DllImport("comctl32.dll")]
		[return: MarshalAs(UnmanagedType.Bool)]
		private static extern bool FlatSB_GetScrollInfo(IntPtr hwnd, int barFlag, ref Win32.ScrollInfo scrollInfo);
		[DllImport("comctl32.dll")]
		private static extern int FlatSB_SetScrollInfo(IntPtr hwnd, int barFlag, ref Win32.ScrollInfo scrollInfo, [MarshalAs(UnmanagedType.Bool)] bool redraw);
		[DllImport("user32.dll", CharSet = CharSet.Auto, EntryPoint = "LoadImage", SetLastError = true)]
		public static extern IntPtr LoadImageCursor(IntPtr hinst, string stName, int nType, int cxDesired, int cyDesired, int nFlags);
		[DllImport("user32.dll")]
		public static extern IntPtr GetDC(IntPtr hwnd);
		[DllImport("user32.dll")]
		public static extern IntPtr GetWindowDC(IntPtr hwnd);
		[DllImport("user32.dll")]
		public static extern int ReleaseDC(IntPtr hwnd, IntPtr hdc);
		[DllImport("gdi32.dll")]
		[return: MarshalAs(UnmanagedType.Bool)]
		public static extern bool DeleteDC(IntPtr hdc);
		[DllImport("gdi32.dll")]
		public static extern int GetDeviceCaps(IntPtr hdc, int index);
		[DllImport("gdi32.dll")]
		public static extern IntPtr SelectObject(IntPtr hdc, IntPtr obj);
		[DllImport("gdi32.dll")]
		[return: MarshalAs(UnmanagedType.Bool)]
		public static extern bool DeleteObject(IntPtr p1);
		[DllImport("gdi32.dll")]
		public static extern IntPtr CreateSolidBrush(int color);
		[DllImport("gdi32.dll")]
		public static extern IntPtr CreatePen(int style, int width, int color);
		[DllImport("gdi32.dll")]
		public static extern IntPtr CreateCompatibleDC(IntPtr hdc);
		[DllImport("gdi32.dll")]
		[return: MarshalAs(UnmanagedType.Bool)]
		public static extern bool SetWorldTransform(IntPtr hdc, ref Win32.XFORM p2);
		[DllImport("gdi32.dll")]
		public static extern int SetTextColor(IntPtr hdc, int color);
		[DllImport("gdi32.dll")]
		public static extern int SetBkColor(IntPtr hdc, int color);
		[DllImport("gdi32.dll")]
		public static extern int SetBkMode(IntPtr hdc, int bkMode);
		[DllImport("gdi32.dll")]
		public static extern int GetGraphicsMode(IntPtr hdc);
		[DllImport("gdi32.dll")]
		public static extern int SetGraphicsMode(IntPtr hdc, int mode);
		[DllImport("gdi32.dll")]
		public static extern IntPtr GetStockObject(int index);
		public static int ColorToGdiColor(Color color)
		{
			int num = color.ToArgb() & 16777215;
			return (num >> 16) + (num & 65280) + ((num & 255) << 16);
		}
		[EnvironmentPermission(SecurityAction.LinkDemand, Unrestricted = true)]
		[DllImport("gdi32.dll", CharSet = CharSet.Auto)]
		[return: MarshalAs(UnmanagedType.Bool)]
		public static extern bool GetTextExtentPoint32(IntPtr hdc, string s, int count, ref Win32.GdiSize size);
		[DllImport("gdi32.dll")]
		public static extern int GetTextMetrics(IntPtr hdc, ref Win32.TextMetrics metrics);
		[DllImport("user32.dll")]
		public static extern int GetSystemMetrics(int nIndex);
		[DllImport("user32.dll")]
		public static extern int GetDoubleClickTime();
		public static bool GetTextExtentPoint32(IntPtr hdc, string s, int count, ref Size size)
		{
			Win32.GdiSize gdiSize = new Win32.GdiSize(0, 0);
			bool textExtentPoint = Win32.GetTextExtentPoint32(hdc, s, count, ref gdiSize);
			size.Width = gdiSize.cx;
			size.Height = gdiSize.cy;
			return textExtentPoint;
		}
		public static bool GetCharABCWidths(IntPtr hdc, int iFirstChar, int iLastChar, ref int[] buffer)
		{
			bool flag = true;
			IntPtr intPtr = Marshal.AllocHGlobal(buffer.Length * 12);
			try
			{
				flag = Win32.GetCharABCWidths(hdc, iFirstChar, iLastChar, intPtr);
				if (flag)
				{
					for (int i = 0; i < buffer.Length; i++)
					{
						buffer[i] = Marshal.ReadInt32(intPtr, i * 12) + Marshal.ReadInt32(intPtr, i * 12 + 4) + Marshal.ReadInt32(intPtr, i * 12 + 8);
					}
				}
			}
			finally
			{
				Marshal.FreeHGlobal(intPtr);
			}
			return flag;
		}
		[DllImport("user32.dll")]
		public static extern int FillRect(IntPtr hdc, ref Win32.GdiRect rect, IntPtr brush);
		[DllImport("user32.dll")]
		public static extern int FrameRect(IntPtr hdc, ref Win32.GdiRect rect, IntPtr brush);
		[DllImport("gdi32.dll")]
		[return: MarshalAs(UnmanagedType.Bool)]
		public static extern bool RoundRect(IntPtr hdc, int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, int nWidth, int nHeight);
		[DllImport("comctl32.dll")]
		[return: MarshalAs(UnmanagedType.Bool)]
		public static extern bool ImageList_DrawEx(IntPtr handle, int index, IntPtr hdc, int x, int y, int dx, int dy, int bk, int fr, int style);
		[EnvironmentPermission(SecurityAction.LinkDemand, Unrestricted = true)]
		[DllImport("gdi32.dll")]
		[return: MarshalAs(UnmanagedType.Bool)]
		public static extern bool PolyBezier(IntPtr hdc, IntPtr points, int count);
		[EnvironmentPermission(SecurityAction.LinkDemand, Unrestricted = true)]
		[DllImport("gdi32.dll")]
		[return: MarshalAs(UnmanagedType.Bool)]
		public static extern bool Polygon(IntPtr hdc, IntPtr points, int count);
		[DllImport("gdi32.dll")]
		[return: MarshalAs(UnmanagedType.Bool)]
		public static extern bool MoveToEx(IntPtr hdc, int x, int y, IntPtr p);
		[DllImport("gdi32.dll")]
		[return: MarshalAs(UnmanagedType.Bool)]
		public static extern bool LineTo(IntPtr hdc, int x, int y);
		[DllImport("gdi32.dll")]
		public static extern int SetPixel(IntPtr hdc, int x, int y, int color);
		public static int FillRect(IntPtr hdc, int x, int y, int w, int h, IntPtr brush)
		{
			Win32.GdiRect gdiRect = new Win32.GdiRect(x, y, x + w, y + h);
			return Win32.FillRect(hdc, ref gdiRect, brush);
		}
		public static int FrameRect(IntPtr hdc, int x, int y, int w, int h, IntPtr brush)
		{
			Win32.GdiRect gdiRect = new Win32.GdiRect(x, y, x + w, y + h);
			return Win32.FrameRect(hdc, ref gdiRect, brush);
		}
		public static bool PolyBezier(IntPtr hdc, Point[] points, int count)
		{
			IntPtr intPtr = Marshal.AllocHGlobal(count * 8);
			bool result;
			try
			{
				for (int i = 0; i < count; i++)
				{
					Marshal.WriteInt32(intPtr, i * 8, points[i].X);
					Marshal.WriteInt32(intPtr, i * 8 + 4, points[i].Y);
				}
				result = Win32.PolyBezier(hdc, intPtr, count);
			}
			finally
			{
				Marshal.FreeHGlobal(intPtr);
			}
			return result;
		}
		public static bool Polygon(IntPtr hdc, Point[] points, int count)
		{
			IntPtr intPtr = Marshal.AllocHGlobal(count * 8);
			bool result;
			try
			{
				for (int i = 0; i < count; i++)
				{
					Marshal.WriteInt32(intPtr, i * 8, points[i].X);
					Marshal.WriteInt32(intPtr, i * 8 + 4, points[i].Y);
				}
				result = Win32.Polygon(hdc, intPtr, count);
			}
			finally
			{
				Marshal.FreeHGlobal(intPtr);
			}
			return result;
		}
		public static int SetPixel(IntPtr hdc, int x, int y, Color color)
		{
			return Win32.SetPixel(hdc, x, y, Win32.ColorToGdiColor(color));
		}
		public static int ExtTextOut(IntPtr hdc, Rectangle rect, int x, int y, int options, string s, int len, int[] spacings)
		{
			Win32.GdiRect gdiRect = new Win32.GdiRect(rect);
			if (len < 0)
			{
				len = s.Length;
			}
			IntPtr intPtr = (spacings != null) ? Marshal.AllocHGlobal(len * 4) : IntPtr.Zero;
			int result;
			try
			{
				if (intPtr != IntPtr.Zero)
				{
					int num = spacings.Length;
					for (int i = 0; i < len; i++)
					{
						Marshal.WriteInt32(intPtr, i * 4, (i < num) ? spacings[i] : 0);
					}
				}
				result = Win32.ExtTextOut(hdc, x, y, options, ref gdiRect, s, len, intPtr);
			}
			finally
			{
				if (intPtr != IntPtr.Zero)
				{
					Marshal.FreeHGlobal(intPtr);
				}
			}
			return result;
		}
        public static bool DrawEdge(IntPtr hdc, ref Rectangle rect, System.Windows.Forms.Border3DStyle edge, System.Windows.Forms.Border3DSide flags)
		{
			Win32.GdiRect gdiRect = new Win32.GdiRect(rect);
			bool result = Win32.DrawEdge(hdc, ref gdiRect, edge, flags);
			rect = new Rectangle(gdiRect.left, gdiRect.top, gdiRect.right - gdiRect.left, gdiRect.bottom - gdiRect.top);
			return result;
		}
		public static int DrawText(IntPtr hdc, string s, int len, ref Rectangle rect, int format)
		{
			Win32.GdiRect gdiRect = new Win32.GdiRect(rect);
			int result = Win32.DrawText(hdc, s, len, ref gdiRect, format);
			rect = gdiRect.ToRectangle();
			return result;
		}
		public static IntPtr CreateFontIndirect(string fontName, FontStyle fontStyle, int fontHeight)
		{
			return Win32.CreateFont(fontHeight, 0, 0, 0, ((fontStyle & FontStyle.Bold) != FontStyle.Regular) ? 700 : 400, ((fontStyle & FontStyle.Italic) != FontStyle.Regular) ? 1u : 0u, ((fontStyle & FontStyle.Underline) != FontStyle.Regular) ? 1u : 0u, ((fontStyle & FontStyle.Strikeout) != FontStyle.Regular) ? 1u : 0u, 1u, 0u, 0u, 0u, 0u, fontName);
		}
		[DllImport("gdi32.dll")]
		public static extern int IntersectClipRect(IntPtr hdc, int l, int t, int r, int b);
		[DllImport("gdi32.dll")]
		public static extern int ExcludeClipRect(IntPtr hdc, int l, int t, int r, int b);
		[DllImport("gdi32.dll")]
		public static extern IntPtr CreateRectRgnIndirect(ref Win32.GdiRect rect);
		[DllImport("gdi32.dll")]
		public static extern int GetClipRgn(IntPtr handle, IntPtr rgn);
		[DllImport("gdi32.dll")]
		public static extern int SelectClipRgn(IntPtr handle, IntPtr rgn);
		public static IntPtr CreateRectRgnIndirect(Rectangle rect)
		{
			Win32.GdiRect gdiRect = new Win32.GdiRect(rect.Left, rect.Top, rect.Right, rect.Bottom);
			return Win32.CreateRectRgnIndirect(ref gdiRect);
		}
		[DllImport("user32.dll")]
		public static extern IntPtr SendMessage(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam);
		[DllImport("user32.dll")]
		[return: MarshalAs(UnmanagedType.Bool)]
		public static extern bool PostMessage(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam);
		[DllImport("user32.dll")]
		public static extern IntPtr SetWindowsHookEx(int idHook, HookHandler lpfn, IntPtr hMod, int dwThreadId);
		[DllImport("user32.dll")]
		[return: MarshalAs(UnmanagedType.Bool)]
		public static extern bool UnhookWindowsHookEx(IntPtr hhk);
		[DllImport("user32.dll")]
		public static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wparam, IntPtr lparam);
		
		
		[DllImport("user32.dll")]
		[return: MarshalAs(UnmanagedType.Bool)]
		public static extern bool SetWindowPos(IntPtr hwnd, IntPtr hwndInsertAfter, int x, int y, int cx, int cy, int uFlags);
		[DllImport("user32.dll")]
		[return: MarshalAs(UnmanagedType.Bool)]
		public static extern bool EnumChildWindows(IntPtr hwndParent, EnumChildProc lpEnumFunc, IntPtr lparam);
		[EnvironmentPermission(SecurityAction.LinkDemand, Unrestricted = true)]
		[DllImport("user32.dll")]
		public static extern int GetClassName(IntPtr hwnd, IntPtr lpClassName, int nMaxCount);
		[DllImport("user32.dll")]
		[return: MarshalAs(UnmanagedType.Bool)]
		public static extern bool MessageBeep(int type);
		[DllImport("kernel32.dll")]
		[return: MarshalAs(UnmanagedType.Bool)]
		public static extern bool Beep(int freq, int duration);
		[DllImport("kernel32.dll")]
		public static extern void Sleep(int milliSeconds);
		[DllImport("uxtheme.dll")]
		public static extern int CloseThemeData(IntPtr htheme);
		[DllImport("uxtheme.dll")]
		[return: MarshalAs(UnmanagedType.Bool)]
		public static extern bool DrawThemeBackground(IntPtr theme, IntPtr hdc, int partID, int stateID, ref Win32.GdiRect rect, IntPtr clipRect);
		[DllImport("uxtheme.dll", CharSet = CharSet.Auto)]
		public static extern int GetCurrentThemeName(IntPtr themeFileName, int maxNameChars, IntPtr colorName, int maxColorName, IntPtr dummy1, IntPtr dummy2);
		[DllImport("uxtheme.dll")]
		public static extern int GetThemeColor(IntPtr htheme, int partID, int stateID, int propID, out Win32.ColorRef colorRef);
		[DllImport("uxtheme.dll")]
		[return: MarshalAs(UnmanagedType.Bool)]
		public static extern bool IsAppThemed();
		[DllImport("uxtheme.dll")]
		[return: MarshalAs(UnmanagedType.Bool)]
		public static extern bool IsThemeActive();
		[DllImport("uxtheme.dll", CharSet = CharSet.Auto)]
		public static extern IntPtr OpenThemeData(IntPtr hwnd, string classList);
		[DllImport("uxtheme.dll")]
		public static extern int GetThemeAppProperties();
		public static string GetText(IntPtr hwnd)
		{
			int num = 255;
			IntPtr intPtr = Marshal.AllocHGlobal(num + 1);
			string result;
			try
			{
				int num2 = (int)Win32.SendMessage(hwnd, 13, (IntPtr)num, intPtr);
				result = ((num2 > 0) ? Marshal.PtrToStringAnsi(intPtr, num2) : string.Empty);
			}
			finally
			{
				Marshal.FreeHGlobal(intPtr);
			}
			return result;
		}
		public static void SetText(IntPtr hwnd, string text)
		{
			Win32.SendMessage(hwnd, 12, IntPtr.Zero, Marshal.StringToHGlobalAnsi(text));
		}
		public static string GetClassName(IntPtr hwnd)
		{
			int num = 255;
			IntPtr intPtr = Marshal.AllocHGlobal(num + 1);
			string result;
			try
			{
				int className = Win32.GetClassName(hwnd, intPtr, num);
				result = ((className > 0) ? Marshal.PtrToStringAnsi(intPtr, className) : string.Empty);
			}
			finally
			{
				Marshal.FreeHGlobal(intPtr);
			}
			return result;
		}
		[DllImport("comctl32.dll")]
		[return: MarshalAs(UnmanagedType.Bool)]
		public static extern bool InitializeFlatSB(IntPtr hwnd);
		[DllImport("comctl32.dll")]
		[return: MarshalAs(UnmanagedType.Bool)]
		public static extern bool UninitializeFlatSB(IntPtr hwnd);
		[DllImport("comctl32.dll")]
		[return: MarshalAs(UnmanagedType.Bool)]
		public static extern bool FlatSB_SetScrollProp(IntPtr hwnd, int index, IntPtr newValue, [MarshalAs(UnmanagedType.Bool)] bool setValue);
		public static void ScrollWindow(IntPtr handle, int x, int y, Rectangle rect)
		{
			Win32.GdiRect gdiRect = new Win32.GdiRect(rect);
			Win32.ScrollWindow(handle, x, y, ref gdiRect, IntPtr.Zero);
		}
		public static void SetScrollSize(IntPtr handle, int code, int minPos, int maxPos, int pageSize)
		{
			Win32.ScrollInfo scrollInfo;
			scrollInfo.cbSize = 28u;
			scrollInfo.fMask = 3u;
			scrollInfo.nMin = minPos;
			scrollInfo.nMax = maxPos;
			scrollInfo.nPage = (uint)pageSize;
			scrollInfo.nPos = 0;
			scrollInfo.nTrackPos = 0;
			Win32.SetScrollInfo(handle, code, ref scrollInfo, true);
		}
		public static void SetScrollPos(IntPtr handle, int code, int pos)
		{
			Win32.ScrollInfo scrollInfo;
			scrollInfo.cbSize = 28u;
			scrollInfo.fMask = 4u;
			scrollInfo.nMin = 0;
			scrollInfo.nMax = 0;
			scrollInfo.nPage = 0u;
			scrollInfo.nPos = pos;
			scrollInfo.nTrackPos = 0;
			Win32.SetScrollInfo(handle, code, ref scrollInfo, true);
		}
		public static int GetScrollPos(IntPtr handle, int code)
		{
			Win32.ScrollInfo scrollInfo;
			scrollInfo.cbSize = 28u;
			scrollInfo.fMask = 16u;
			scrollInfo.nMin = 0;
			scrollInfo.nMax = 0;
			scrollInfo.nPage = 0u;
			scrollInfo.nPos = 0;
			scrollInfo.nTrackPos = 0;
			Win32.GetScrollInfo(handle, code, ref scrollInfo);
			return scrollInfo.nTrackPos;
		}
		public static int GetScrollSize(IntPtr handle, int code)
		{
			Win32.ScrollInfo scrollInfo;
			scrollInfo.cbSize = 28u;
			scrollInfo.fMask = 3u;
			scrollInfo.nMin = 0;
			scrollInfo.nMax = 0;
			scrollInfo.nPage = 0u;
			scrollInfo.nPos = 0;
			scrollInfo.nTrackPos = 0;
			Win32.GetScrollInfo(handle, code, ref scrollInfo);
			return (int)(((ulong)scrollInfo.nPage > (ulong)((long)scrollInfo.nMax)) ? 4294967295u : scrollInfo.nPage);
		}
		public static void SetScrollBarInfo(IntPtr handle, int size, int pageSize, int code)
		{
			if (size < pageSize)
			{
				size = pageSize;
			}
			Win32.SetScrollSize(handle, code, 0, size, pageSize);
		}
		public static void FlatSB_SetScrollSize(IntPtr handle, int code, int minPos, int maxPos, int pageSize)
		{
			Win32.ScrollInfo scrollInfo;
			scrollInfo.cbSize = 28u;
			scrollInfo.fMask = 3u;
			scrollInfo.nMin = minPos;
			scrollInfo.nMax = maxPos;
			scrollInfo.nPage = (uint)pageSize;
			scrollInfo.nPos = 0;
			scrollInfo.nTrackPos = 0;
			Win32.FlatSB_SetScrollInfo(handle, code, ref scrollInfo, true);
		}
		public static void FlatSB_SetScrollPos(IntPtr handle, int code, int pos)
		{
			Win32.ScrollInfo scrollInfo;
			scrollInfo.cbSize = 28u;
			scrollInfo.fMask = 4u;
			scrollInfo.nMin = 0;
			scrollInfo.nMax = 0;
			scrollInfo.nPage = 0u;
			scrollInfo.nPos = pos;
			scrollInfo.nTrackPos = 0;
			Win32.FlatSB_SetScrollInfo(handle, code, ref scrollInfo, true);
		}
		public static int FlatSB_GetScrollPos(IntPtr handle, int code)
		{
			Win32.ScrollInfo scrollInfo;
			scrollInfo.cbSize = 28u;
			scrollInfo.fMask = 16u;
			scrollInfo.nMin = 0;
			scrollInfo.nMax = 0;
			scrollInfo.nPage = 0u;
			scrollInfo.nPos = 0;
			scrollInfo.nTrackPos = 0;
			Win32.FlatSB_GetScrollInfo(handle, code, ref scrollInfo);
			return scrollInfo.nTrackPos;
		}
		public static int FlatSB_GetScrollSize(IntPtr handle, int code)
		{
			Win32.ScrollInfo scrollInfo;
			scrollInfo.cbSize = 28u;
			scrollInfo.fMask = 3u;
			scrollInfo.nMin = 0;
			scrollInfo.nMax = 0;
			scrollInfo.nPage = 0u;
			scrollInfo.nPos = 0;
			scrollInfo.nTrackPos = 0;
			Win32.FlatSB_GetScrollInfo(handle, code, ref scrollInfo);
			return (int)(((ulong)scrollInfo.nPage > (ulong)((long)scrollInfo.nMax)) ? 4294967295u : scrollInfo.nPage);
		}
		public static void FlatSB_SetScrollBarInfo(IntPtr handle, int size, int pageSize, int code)
		{
			if (size < pageSize)
			{
				size = pageSize;
			}
			Win32.FlatSB_SetScrollSize(handle, code, 0, size, pageSize);
		}
		[DllImport("Imm32.dll")]
		public static extern IntPtr ImmGetContext(IntPtr hWnd);
		[DllImport("Imm32.dll")]
		[return: MarshalAs(UnmanagedType.Bool)]
		public static extern bool ImmGetOpenStatus(IntPtr hIMC);
		[DllImport("Imm32.dll")]
		[return: MarshalAs(UnmanagedType.Bool)]
		public static extern bool ImmReleaseContext(IntPtr hWnd, IntPtr hIMC);
		[DllImport("Imm32.dll", CharSet = CharSet.Auto)]
		[return: MarshalAs(UnmanagedType.I4)]
		public static extern int ImmGetCompositionString(IntPtr hImc, int dwIndex, ref string lpBuf, int dwLen);
		[DllImport("Imm32.dll", CharSet = CharSet.Auto)]
		[return: MarshalAs(UnmanagedType.Bool)]
		public static extern bool ImmSetCompositionWindow(IntPtr hIMC, ref Win32.COMPOSITIONFORM lpCompForm);
		[DllImport("Imm32.dll", CharSet = CharSet.Auto)]
		[return: MarshalAs(UnmanagedType.Bool)]
		public static extern bool ImmSetCompositionFont(IntPtr hIMC, Win32.LOGFONT logFont);
		public static void ImmSetCompositionWindow(IntPtr hWnd, Point pos)
		{
			Win32.GdiPoint ptCurrentPos = new Win32.GdiPoint(pos.X, pos.Y);
			Win32.COMPOSITIONFORM cOMPOSITIONFORM = default(Win32.COMPOSITIONFORM);
			IntPtr hIMC = Win32.ImmGetContext(hWnd);
			try
			{
				if (Win32.ImmGetOpenStatus(hIMC))
				{
					cOMPOSITIONFORM.dwStyle = 2u;
					cOMPOSITIONFORM.ptCurrentPos = ptCurrentPos;
					cOMPOSITIONFORM.rcArea = new Win32.GdiRect(0, 0, 0, 0);
					Win32.ImmSetCompositionWindow(hIMC, ref cOMPOSITIONFORM);
				}
			}
			finally
			{
				Win32.ImmReleaseContext(hWnd, hIMC);
			}
		}
		public static void UpdateCompositionWindow(IntPtr hWnd, Point pos, IntPtr wndPos)
		{
			object obj = Marshal.PtrToStructure(wndPos, typeof(Win32.COMPOSITIONFORM));
			if (obj != null) {
			    Win32.COMPOSITIONFORM obj2 = (Win32.COMPOSITIONFORM) obj;
				obj2.ptCurrentPos = new Win32.GdiPoint(pos.X, pos.Y);
			}
		}
		public static void ImeComposition(IntPtr hWnd)
		{
			string text = string.Empty;
			IntPtr intPtr = Win32.ImmGetContext(hWnd);
			try
			{
				if (intPtr != IntPtr.Zero)
				{
					int num = Win32.ImmGetCompositionString(intPtr, 2048, ref text, 0);
					if (num > 0)
					{
						text = new string(' ', num);
						Win32.ImmGetCompositionString(intPtr, 2048, ref text, num);
					}
				}
			}
			finally
			{
				Win32.ImmReleaseContext(hWnd, intPtr);
			}
		}
		public static void ImmSetCompositionFont(IntPtr hWnd, Font font)
		{
			IntPtr hIMC = Win32.ImmGetContext(hWnd);
			try
			{
				if (Win32.ImmGetOpenStatus(hIMC))
				{
					Win32.LOGFONT logFont = new Win32.LOGFONT();
					font.ToLogFont(logFont);
					Win32.ImmSetCompositionFont(hIMC, logFont);
				}
			}
			finally
			{
				Win32.ImmReleaseContext(hWnd, hIMC);
			}
		}
	}
}
